//
//  ChaKanBiXingShouCangDanXuanTiViewController.swift
//  CKC
//
//  Created by 张振东 on 2016/10/22.
//  Copyright © 2016年 ZZD. All rights reserved.
//

import UIKit
import RealmSwift

class ChaKanBiXingShouCangDanXuanTiViewController: UIViewController {

    let screenWidth = UIScreen.main.bounds.width
    let screenHeight = UIScreen.main.bounds.height
    var navigationHeight:CGFloat?
    
    //定义一个变量:记录题号的值,默认为1
    var tiHao:Int = 1

    var leftTopImageView:UIImageView?
    var rightTopImageView:UIImageView?
    var leftDownImageView:UIImageView?
    var rightDownImageView:UIImageView?
    
    //定义四个lable对象,记录A、B、C、D
    var buttonA:UIButton?
    var buttonB:UIButton?
    var buttonC:UIButton?
    var buttonD:UIButton?
    
    //定义三个label,用于记录题干的内容
    var tiHaoLabel:UILabel?
    
    var tiGanNeiRongLabel:UILabel?
    
    var randomNumberLable:UILabel?
    
    var diShiLabel:UILabel?
    
    var zuoKuoHaoLabel:UILabel?
    var selectedOptionLabel:UILabel?
    var youKuoHaoLabel:UILabel?
    
    //定义一个按钮:下一题
    var nextQuestionBtn:UIButton?
    
    //保存从数据库中查询出来的结果集
    var biXingDanXuanTiItems:Results<BiXingDanXuanTi>?
    
    //定义一个变量,存储收藏的题目的个数
    var biXingDanXuanTiItemsCount:Int = 0
    
    //定义一个变量,初始化为0
    var index:Int = 0
    
    //定义一个变量,用于保存随机生成的图片的名称
    var leftTopImageName:String?
    var rightTopImageName:String?
    var leftDownImageName:String?
    var rightDownImageName:String?
    
    var leftTopImage:UIImage?
    var rightTopImage:UIImage?
    var leftDownImage:UIImage?
    var rightDownImage:UIImage?
    
    var leftTopImageViewBiXingCode = 0
    var rightTopImageViewBiXingCode = 0
    var leftDownImageViewBiXingCode = 0
    var rightDownImageViewBiXingCode = 0
    
    
    override func viewDidLoad() {
        
        //使用默认的数据库
        let realm = try! Realm()
        //查询所有的消费记录
        biXingDanXuanTiItems = realm.objects(BiXingDanXuanTi.self)
        
        print(biXingDanXuanTiItems as Any)
        biXingDanXuanTiItemsCount = (biXingDanXuanTiItems?.count)!
        self.title = "我的收藏"
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white,NSFontAttributeName:UIFont(name: "STHeitiSC-Medium", size: 20.0)!]
        
        self.view.backgroundColor = UIColor(red: 246,green: 246,blue: 246,alpha: 1)
        let leftBarButton = UIButton(frame:CGRect(x: 0,y: 0,width: 20,height: 20))
        leftBarButton.setBackgroundImage(UIImage(named: "fanhui"), for: UIControlState())
        leftBarButton.addTarget(self, action: #selector(backToPrevious), for: .touchUpInside)
        let leftBarButtonItem = UIBarButtonItem(customView: leftBarButton)
        self.navigationItem.leftBarButtonItems = [leftBarButtonItem]
        
        //设置右边的按钮
        let rightBarButton = UIButton(frame:CGRect(x: screenWidth-20,y: 0,width: 20,height: 20))
        rightBarButton.setBackgroundImage(UIImage(named: "qingkongshoucang"), for: UIControlState())
        let rightBarButtonItem = UIBarButtonItem(customView: rightBarButton)
        rightBarButton.addTarget(self, action: #selector(qingKongShouCang), for: .touchUpInside)
        
        self.navigationItem.rightBarButtonItems = [rightBarButtonItem]

        fengZhuangKongJianShuXing()
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    
    func fengZhuangKongJianShuXing(){
        //获取导航栏的高度
        navigationHeight = self.navigationController?.navigationBar.bounds.size.height
        
        //初始化题干label对象
        tiHaoLabel = UILabel(frame:CGRect(x: 10,y: navigationHeight!+30,width: 20,height: 30))
        fengZhuangLabelAttributes(tiHaoLabel!)
        
        //初始化题干内容label对象
        tiGanNeiRongLabel = UILabel(frame:CGRect(x: 20,y: navigationHeight!+30,width: 210,height: 30))
        fengZhuangLabelAttributes(tiGanNeiRongLabel!)
        
        //初始化随机生成数label对象
        randomNumberLable = UILabel(frame:CGRect(x: 20+5+205,y: navigationHeight!+30,width: 20,height: 30))
        fengZhuangLabelAttributes(randomNumberLable!)
        
        
        //初始化"的是"label对象
        diShiLabel = UILabel(frame:CGRect(x: 20+10+205+10,y: navigationHeight!+30,width: 40,height: 30))
        fengZhuangLabelAttributes(diShiLabel!)
        
        //初始化左括号label对象
        zuoKuoHaoLabel = UILabel(frame:CGRect(x: 20+10+205+10+40,y: navigationHeight!+30,width: 10,height: 30))
        fengZhuangLabelAttributes(zuoKuoHaoLabel!)
        
        //初始选中的选项Label对象
        selectedOptionLabel = UILabel(frame:CGRect(x: 20+10+205+10+40+10,y: navigationHeight!+30,width: 15,height: 30))
        fengZhuangLabelAttributes(selectedOptionLabel!)
        
        //初始化右括号label对象
        youKuoHaoLabel = UILabel(frame:CGRect(x: 20+10+205+10+40+10+15,y: navigationHeight!+30,width: 10,height: 30))
        fengZhuangLabelAttributes(youKuoHaoLabel!)
        
        //初始化四个imageView
        leftTopImageView = UIImageView(frame:CGRect(x:10,y:navigationHeight!+30+40,width:(screenWidth-40)/2,height:(screenWidth-40)/2))
        leftTopImageView!.contentMode = .scaleAspectFit
        
        rightTopImageView = UIImageView(frame:CGRect(x:screenWidth/2+10,y:navigationHeight!+70,width:(screenWidth-40)/2,height:(screenWidth-40)/2))
        rightTopImageView!.contentMode = .scaleAspectFit
        
        leftDownImageView = UIImageView(frame:CGRect(x:10,y:navigationHeight!+105+(screenWidth-40)/2,width:(screenWidth-40)/2,height:(screenWidth-40)/2))
        leftDownImageView!.contentMode = .scaleAspectFit
        
        rightDownImageView = UIImageView(frame:CGRect(x:screenWidth/2+10,y:navigationHeight!+105+(screenWidth-40)/2,width:(screenWidth-40)/2,height:(screenWidth-40)/2))
        rightDownImageView!.contentMode = .scaleAspectFit
        
        //初始化四个按钮
        buttonA = UIButton(frame:CGRect(x: 10+((screenWidth-40)/2-40)/2,y: navigationHeight!+70+(screenWidth-40)/2,width: 60,height: 20))
        fengZhuangButtonAttributes(buttonA!)
        
        buttonB = UIButton(frame:CGRect(x: screenWidth/2+10+((screenWidth-40)/2-40)/2,y: navigationHeight!+70+(screenWidth-40)/2,width: 60,height: 20))
        fengZhuangButtonAttributes(buttonB!)
        
        buttonC = UIButton(frame:CGRect(x: 10+((screenWidth-40)/2-40)/2,y: navigationHeight!+105+screenWidth-40,width: 60,height: 20))
        fengZhuangButtonAttributes(buttonC!)
        
        buttonD = UIButton(frame:CGRect(x: screenWidth/2+10+((screenWidth-40)/2-40)/2,y: navigationHeight!+105+screenWidth-40,width: 60,height: 20))
        fengZhuangButtonAttributes(buttonD!)
    
        nextQuestionBtn = UIButton(frame:CGRect(x: 10,y: screenHeight-50,width: screenWidth-20,height: 40))
        //初始化下一题按钮
        fengZhuangButtonAttributes(nextQuestionBtn!)
        
        if biXingDanXuanTiItemsCount == 0{
            fengZhuangYaoXianShiDeKongJianWeiLingShi()
        }else{
            fengZhuangYaoXianShiDeKongJian()
            
            self.tiHaoLabel?.text = biXingDanXuanTiItems![index].tiHao
            self.randomNumberLable?.text = biXingDanXuanTiItems![index].randomNumber
            self.selectedOptionLabel?.text = biXingDanXuanTiItems![index].selectedOptionValue
            leftTopImageName = biXingDanXuanTiItems![index].leftTopImageName
            rightTopImageName = biXingDanXuanTiItems![index].rightTopImageName
            leftDownImageName = biXingDanXuanTiItems![index].leftDownImageName
            rightDownImageName = biXingDanXuanTiItems![index].rightDownImageName
            
            leftTopImageViewBiXingCode = Int(((leftTopImageName! as NSString)).substring(with: NSMakeRange(0, 1)))!
            rightTopImageViewBiXingCode = Int(((rightTopImageName! as NSString)).substring(with: NSMakeRange(0, 1)))!
            leftDownImageViewBiXingCode = Int(((leftDownImageName! as NSString)).substring(with: NSMakeRange(0, 1)))!
            rightDownImageViewBiXingCode = Int(((rightDownImageName! as NSString)).substring(with: NSMakeRange(0, 1)))!
            print(leftTopImageViewBiXingCode)
            print(rightTopImageViewBiXingCode)
            print(leftDownImageViewBiXingCode)
            print(rightDownImageViewBiXingCode)
            
            if biXingDanXuanTiItems![index].selectedOptionValue == "A"{
                if leftTopImageViewBiXingCode == Int(biXingDanXuanTiItems![index].randomNumber!)!{
                    buttonA?.backgroundColor = UIColor.green
                }else{
                    buttonB!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                    buttonC!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                    buttonD!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                }
                
            }
            
            if biXingDanXuanTiItems![index].selectedOptionValue == "B"{
                if rightTopImageViewBiXingCode == Int(biXingDanXuanTiItems![index].randomNumber!)!{
                    buttonB?.backgroundColor = UIColor.green
                    
                }else{
                    buttonB!.backgroundColor = UIColor.gray
                    buttonA!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                    buttonC!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                    buttonD!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                }
            }
            
            if biXingDanXuanTiItems![index].selectedOptionValue == "C"{
                if leftDownImageViewBiXingCode == Int(biXingDanXuanTiItems![index].randomNumber!)!{
                    buttonC?.backgroundColor = UIColor.green
                    
                }else{
                    buttonC!.backgroundColor = UIColor.gray
                    buttonA!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                    buttonB!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                    buttonD!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                }
            }
            
            if biXingDanXuanTiItems![index].selectedOptionValue == "D"{
                if rightDownImageViewBiXingCode == Int(biXingDanXuanTiItems![index].randomNumber!)!{
                    buttonD?.backgroundColor = UIColor.green
                    
                }else{
                    buttonD!.backgroundColor = UIColor.gray
                    buttonA!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                    buttonB!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                    buttonC!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                }
            }
            

            leftTopImage = UIImage(named: leftTopImageName!)
            rightTopImage = UIImage(named: rightTopImageName!)
            leftDownImage = UIImage(named: leftDownImageName!)
            rightDownImage = UIImage(named: rightDownImageName!)
            
            leftTopImageView?.image = leftTopImage
            rightTopImageView?.image = rightTopImage
            leftDownImageView?.image = leftDownImage
            rightDownImageView?.image = rightDownImage
            //点击下一题进行更新传递过来的7个字符串
            nextQuestionBtn?.addTarget(self, action: #selector(clickNextQuestion), for: .touchUpInside)
        }
        
        
    }
    
    /*
        作者:张振东
        日期:2017-10-20
        功能:当收藏的个数>0时,显示所有控件
     */
    func fengZhuangYaoXianShiDeKongJian(){
        self.view.addSubview(tiHaoLabel!)
        self.view.addSubview(tiGanNeiRongLabel!)
        self.view.addSubview(randomNumberLable!)
        self.view.addSubview(diShiLabel!)
        self.view.addSubview(zuoKuoHaoLabel!)
        self.view.addSubview(selectedOptionLabel!)
        self.view.addSubview(youKuoHaoLabel!)
        self.view.addSubview(leftTopImageView!)
        self.view.addSubview(rightTopImageView!)
        self.view.addSubview(leftDownImageView!)
        self.view.addSubview(rightDownImageView!)
        self.view.addSubview(buttonA!)
        self.view.addSubview(buttonB!)
        self.view.addSubview(buttonC!)
        self.view.addSubview(buttonD!)
        self.view.addSubview(nextQuestionBtn!)
    }
    
    /*
     作者:张振东
     日期:2017-10-20
     功能:当收藏的个数=0时,显示所有空间
     */
    func fengZhuangYaoXianShiDeKongJianWeiLingShi(){
        
    }
    
    func qingKongShouCang(){
        print(index)
        qingChuDangQianShouCangTi(index)
        
    }
    
    /*
     作者:张振东
     日期:2016-10-23
     功能:清空当前页面的收藏
     */
    func qingChuDangQianShouCangTi(_ index:Int){
        if index < biXingDanXuanTiItemsCount{
            //使用默认的数据库
            let realm = try! Realm()
            try! realm.write{
                realm.delete(biXingDanXuanTiItems![index])
            }
            //biXingTianKongTiItemsCount要跟着发生变化,数量减1
            biXingDanXuanTiItemsCount = biXingDanXuanTiItemsCount - 1
            //弹出一个提示框,显示没有收藏题了
            let alertController = UIAlertController(title: "温馨提示",message: "亲,清除成功!",preferredStyle: .alert)
            let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
            let okAction = UIAlertAction(title: "好的", style: .default, handler: {
                action in
                print("点击了确定")
            })
            alertController.addAction(cancelAction)
            alertController.addAction(okAction)
            self.present(alertController, animated: true, completion: nil)
        }
    }

    
    /*
        作者:张振东
        日期:2017-10-20
        功能:点击下一个收藏的时候,重新生成新的图片
     */
    func clickNextQuestion(){
       
        index = index + 1
        print(index)
        if index < biXingDanXuanTiItemsCount{
            gengXinShouCangTi(index)
        }else{
            index = biXingDanXuanTiItemsCount - 1
        }
    }
    
    
    /*
     作者:张振东
     日期:2017-10-20
     功能:该方法实现根据不同的index,显示不同的收藏题
     */
    func gengXinShouCangTi(_ index:Int){
        if index < biXingDanXuanTiItemsCount{
            self.tiHaoLabel?.text = biXingDanXuanTiItems![index].tiHao
            self.randomNumberLable?.text = biXingDanXuanTiItems![index].randomNumber
            self.selectedOptionLabel?.text = biXingDanXuanTiItems![index].selectedOptionValue
            leftTopImageName = biXingDanXuanTiItems![index].leftTopImageName
            rightTopImageName = biXingDanXuanTiItems![index].rightTopImageName
            leftDownImageName = biXingDanXuanTiItems![index].leftDownImageName
            rightDownImageName = biXingDanXuanTiItems![index].rightDownImageName
            
            leftTopImageViewBiXingCode = Int(((leftTopImageName! as NSString)).substring(with: NSMakeRange(0, 1)))!
            rightTopImageViewBiXingCode = Int(((rightTopImageName! as NSString)).substring(with: NSMakeRange(0, 1)))!
            leftDownImageViewBiXingCode = Int(((leftDownImageName! as NSString)).substring(with: NSMakeRange(0, 1)))!
            rightDownImageViewBiXingCode = Int(((rightDownImageName! as NSString)).substring(with: NSMakeRange(0, 1)))!
            print(leftTopImageViewBiXingCode)
            print(rightTopImageViewBiXingCode)
            print(leftDownImageViewBiXingCode)
            print(rightDownImageViewBiXingCode)
            
            if biXingDanXuanTiItems![index].selectedOptionValue == "A"{
                if leftTopImageViewBiXingCode == Int(biXingDanXuanTiItems![index].randomNumber!)!{
                    buttonA?.backgroundColor = UIColor.green
                    buttonB!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                    buttonC!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                    buttonD!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                }else{
                    buttonA?.backgroundColor = UIColor.gray
                    buttonB!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                    buttonC!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                    buttonD!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                }
            
            }
            
            if biXingDanXuanTiItems![index].selectedOptionValue == "B"{
                if rightTopImageViewBiXingCode == Int(biXingDanXuanTiItems![index].randomNumber!)!{
                    buttonB?.backgroundColor = UIColor.green
                    buttonA!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                    buttonC!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                    buttonD!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                }else{
                    buttonB!.backgroundColor = UIColor.gray
                    buttonA!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                    buttonC!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                    buttonD!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                }
            }
            
            if biXingDanXuanTiItems![index].selectedOptionValue == "C"{
                if leftDownImageViewBiXingCode == Int(biXingDanXuanTiItems![index].randomNumber!)!{
                    buttonC?.backgroundColor = UIColor.green
                    buttonA!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                    buttonB!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                    buttonD!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                }else{
                    buttonC!.backgroundColor = UIColor.gray
                    buttonA!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                    buttonB!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                    buttonD!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                }
            }
            
            if biXingDanXuanTiItems![index].selectedOptionValue == "D"{
                if rightDownImageViewBiXingCode == Int(biXingDanXuanTiItems![index].randomNumber!)!{
                    buttonD?.backgroundColor = UIColor.green
                    buttonA!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                    buttonB!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                    buttonC!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                }else{
                    buttonD!.backgroundColor = UIColor.gray
                    buttonA!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                    buttonB!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                    buttonC!.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
                }
            }
            leftTopImage = UIImage(named: leftTopImageName!)
            rightTopImage = UIImage(named: rightTopImageName!)
            leftDownImage = UIImage(named: leftDownImageName!)
            rightDownImage = UIImage(named: rightDownImageName!)
            
            leftTopImageView?.image = leftTopImage
            rightTopImageView?.image = rightTopImage
            leftDownImageView?.image = leftDownImage
            rightDownImageView?.image = rightDownImage

        }else{
            //弹出一个提示框,显示没有收藏题了
            let alertController = UIAlertController(title: "温馨提示",
                                                    message: "亲,没有下一题了", preferredStyle: .alert)
            let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
            let okAction = UIAlertAction(title: "好的", style: .default, handler: {
                action in
                print("点击了确定")
            })
            alertController.addAction(cancelAction)
            alertController.addAction(okAction)
            self.present(alertController, animated: true, completion: nil)
        }
    }
    
    
    /*
     作者:张振东
     日期:2017-10-20
     功能:封装lable属性
     */
    func fengZhuangLabelAttributes(_ label:UILabel){
        label.font = UIFont(name: "FontAwesome",size: 15)
        switch label {
        case tiHaoLabel!:
            label.text = "\(tiHao)"
            label.font = UIFont(name: "FontAwesome",size: 20)
            break
        case tiGanNeiRongLabel!:
            label.text = "、以下笔形对应的笔形码为"
            break
        case randomNumberLable!:
            label.font = UIFont(name: "FontAwesome",size: 17)
            if biXingDanXuanTiItemsCount == 0{
            }else{
                label.text = "\(String(describing: biXingDanXuanTiItems![index].tiHao))"
            }
            break
        case diShiLabel!:
            label.text = "的是:"
            break
        case zuoKuoHaoLabel!:
            label.text = "("
            break
        case selectedOptionLabel!:
            label.text = ""
            break
        case youKuoHaoLabel!:
            youKuoHaoLabel?.text = ")"
            break
        default:
            print("封装完毕")
        }
    }

    /*
     作者:张振东
     日期:2017-10-20
     功能:封装button属性
     */
    func fengZhuangButtonAttributes(_ button:UIButton){
        button.setTitleColor(UIColor.black, for: UIControlState())
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
        button.layer.cornerRadius = 5
        switch button {
        case buttonA!:
            button.setTitle("A", for: UIControlState())
            break
        case buttonB!:
            button.setTitle("B", for: UIControlState())
            break
        case buttonC!:
            button.setTitle("C", for: UIControlState())
            break
        case buttonD!:
            button.setTitle("D", for: UIControlState())
            break
        case nextQuestionBtn!:
            button.setTitle("下一个", for: UIControlState())
            break
        default:
            print("封装button")
        }
    }

    func backToPrevious(){
        self.navigationController?.popViewController(animated: true)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
